package com.wwwarehouse.resource_center.fragment.createobject.createstore;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.activity.map.MapActivity;
import com.wwwarehouse.common.bean.map.NearAddressBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.AddressEvent;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.greenrobot.event.EventBus;

import static android.app.Activity.RESULT_OK;

/**
 * Description:
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/18 19:42
 */
public class StoreAddressFragment extends TheParentFragment implements View.OnClickListener {

    private LinearLayout mLLMap;
    private LinearLayout mLLIcon;
    private TextView mAddress;
    private EditText mDetailAddress;
    private static int MAPREQUEST_CODE = 0;
    private AutoClickButton mConfirm;
    private NearAddressBean bean;

    @Override
    protected int getContentId() {
        return R.layout.fragment_create_address;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof StoreAddressFragment)
            mActivity.setTitle(R.string.add_store_address);
    }


    @Override
    protected void init(View view) {
        mLLMap = (LinearLayout) view.findViewById(R.id.ll_map);
        mLLIcon = (LinearLayout) view.findViewById(R.id.ll_icon);
        mAddress = (TextView) view.findViewById(R.id.tv_address);
        mDetailAddress = (EditText) view.findViewById(R.id.add_detail_address);
        mDetailAddress.setBackground(null);
        mConfirm = (AutoClickButton) view.findViewById(R.id.confirm_btn);

        ((BaseActivity) mActivity).hideSoftByEditViewIds(new int[]{R.id.add_detail_address});

        mLLMap.setOnClickListener(this);
        mLLIcon.setOnClickListener(this);
        mAddress.setOnClickListener(this);
        mConfirm.setOnClickListener(this);
        mConfirm.bindEditText(mDetailAddress);
        mConfirm.bindEditText(mAddress);

        Bundle bundle = getArguments();
        if (bundle != null) {
            bean = (NearAddressBean) bundle.getSerializable("address");
            assert bean != null;
            mLLIcon.setVisibility(View.GONE);
            mAddress.setVisibility(View.VISIBLE);
            mAddress.setText(bean.getFormatAddress());
            mDetailAddress.setText(bean.getAddress());
        }
        setEditTextInhibitInputSpeChat(mDetailAddress);

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.ll_map || i == R.id.ll_icon || i == R.id.tv_address) {
            startActivityForResult(new Intent(getContext(), MapActivity.class), MAPREQUEST_CODE);
        } else if (i == R.id.confirm_btn) {
            //完成
            bean.setAddress(mDetailAddress.getText().toString());
            EventBus.getDefault().post(new AddressEvent(bean));
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == MAPREQUEST_CODE) {

            mLLIcon.setVisibility(View.GONE);
            mAddress.setVisibility(View.VISIBLE);
            bean = (NearAddressBean) data.getSerializableExtra("address_bean");
            mAddress.setText(bean.getFormatAddress());
            mDetailAddress.setText(bean.getAddress());
        }
    }


    /**
     * 禁止EditText输入特殊字符
     *
     * @param editText
     */
    public static void setEditTextInhibitInputSpeChat(EditText editText) {

        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String speChat = "[`~!@#$%^&*+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());
                if (matcher.find()) return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }


}
